package com.runfast.waimai.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * ${DESCRIPTION} 获取过去7天，一个月，三个月，一年的时间,根据字符串获取时分的时间
 *
 * @author chenzheng
 * @create 2018-05-22 15:52
 **/
public class TimeUtil {

    /*public static void main(String argsp[]) throws Exception{
        String time="11:10";

        Date date=null;
        SimpleDateFormat formatter=new SimpleDateFormat("HH:mm");
        date=formatter.parse(time);
        System.out.println(date);
    }*/
    /* public static void main(String argsp[]) throws Exception{
         Date date=new Date();
         SimpleDateFormat formatter=new SimpleDateFormat("HH:mm");
         String time=formatter.format(date);
         System.out.println(time);

     }*/
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static Calendar c = Calendar.getInstance();
    public static Date getDay7()  throws ParseException{
       //过去七天
        c.setTime(MyUtil.getStartDay(new Date()));
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        String day = format.format(d);
        System.out.println("过去七天："+day);
        return format.parse(day);
    }

    public static Date getMon() throws ParseException{
        //过去一月
        c.setTime(MyUtil.getStartDay(new Date()));
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
        System.out.println("过去一个月："+mon);
        return format.parse(mon);
    }
    public static Date getMon3() throws ParseException{
        //过去三个月
        c.setTime(MyUtil.getStartDay(new Date()));
        c.add(Calendar.MONTH, -3);
        Date m3 = c.getTime();
        String mon3 = format.format(m3);
        System.out.println("过去三个月："+mon3);
        return format.parse(mon3);
    }

    public static Date getYear() throws ParseException{
        //过去一年
        c.setTime(MyUtil.getStartDay(new Date()));
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        System.out.println("过去一年："+year);
        return format.parse(year);
    }

   public static Date getHourAndMinute(String time) throws ParseException{
       Date date=null;
       SimpleDateFormat formatter=new SimpleDateFormat("HH:mm");
       date=formatter.parse(time);
       return date;
    }

    public static String getHourAndMinuteToString(Date time){
        SimpleDateFormat formatter=new SimpleDateFormat("HH:mm");
        String time1=formatter.format(time);
        return time1;
    }
    public static Date getDate(String time) throws ParseException{
        Date date=null;
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date=formatter.parse(time);
        return date;
    }
    public static String getDateToString(Date time) throws ParseException{
        Date date=null;
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time1=formatter.format(time);
        return time1;
    }
    public static String getHourToString(Date time){
        SimpleDateFormat formatter=new SimpleDateFormat("HH:mm");
        String time1=formatter.format(time);
        return time1;
    }
}
